package ru.mcfr.oxygen.updater.utils;

import org.apache.log4j.Logger;

import java.io.*;

public class FileCommander {

    private static Logger logger = Logger.getLogger(FileCommander.class);

    public static boolean deleteDirectory(File path) {
		if (path.exists())
			for (File file : path.listFiles())
				if (file.isDirectory())
					deleteDirectory(file);
				else
					file.delete();
		return path.delete();
	}

    public static String getRelativePath(File base, File entry){
        String basePath = base.getAbsolutePath();
        String entryPath = entry.getAbsolutePath();
        return entryPath.replace(basePath, "");
    }

    public static void copyDirectory(File src , File dest) throws IOException {
        if (src.isDirectory()){
            if (!dest.exists())
                dest.mkdirs();
            for (String children : src.list())
                copyDirectory(new File(src, children), new File(dest, children));
        }
        else if (!src.getName().endsWith(".zip"))
            Streamer.bufferedStreamCopy(new FileInputStream(src), new FileOutputStream(dest));
    }

    public static void copyDirectory(String src , String dest) throws IOException {
        copyDirectory(new File(src), new File(dest));
    }
}
